package com.thingsgrid.firmware.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thingsgrid.appmanager.util.GetID;
import com.thingsgrid.firmware.entity.Firmware;
import com.thingsgrid.firmware.entity.FirmwareUpgradeMode;
import com.thingsgrid.firmware.service.IBatchupgradeService;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/batchupgrade")
@AllArgsConstructor
public class BatchupgradeController {
	private MongoTemplate template;
	private IBatchupgradeService service;

	@GetMapping("/getProduct")
	public R<List> getProduct() {
		return R.data(service.getProduct());
	}

	@PostMapping("/getNowFirmversion")
	public R<List> getFirmversion(String product_id, String upgrade_type) {
		return R.data(service.getFirmversion(product_id, upgrade_type));
	}

	@PostMapping("/getWantedVersion")
	public R<List> getWantedVersion(String product_id, String upgrade_type, String firmware_version) {
		return R.data(service.getWantedVersion(product_id,upgrade_type, firmware_version));
	}

	@PostMapping("/getDevicelist")
	public R<Map> getDeviceList(Page page, @RequestParam("product_id") String product_id, @RequestParam("firmware_version") String firmware_version) {
		return R.data(service.getDeviceList(page, product_id, firmware_version));
	}
	//批量升级的保存接口
	@PostMapping("/saveUpgrade")
	public R saveUpgrade(String content, String cut_size, String product_id, String oldversion, String newversion, Integer upgrade_range, String device_list, int upgradetime_type, Integer upgradetime, String retry, Integer retrytime) {
		/**
		 * 向升级方式表中插入数据
		 */
		FirmwareUpgradeMode firmwareUpgradeMode = new FirmwareUpgradeMode();
		Query query = new Query(Criteria.where("product_id").is(product_id).and("firmware_version").is(oldversion));
		String firmware_id = template.findOne(query, Firmware.class, "firmware").getFirmwareId();
		firmwareUpgradeMode.setMethodContent(content);
		firmwareUpgradeMode.setFirmwareId(firmware_id);
		firmwareUpgradeMode.setNewFirmwareversion(newversion);
		GetID getid = new GetID();
		firmwareUpgradeMode.setMethodId(getid.getTemp());

		List<Map> exitDevices = service.finddevice(firmware_id,oldversion,newversion);
		List<String> devices = new LinkedList<>();
		for(Map map : exitDevices){
			devices.add((String) map.get("device_id"));
		}
		//upgrade_range : 0 全部 1 指定
		StringBuilder sb = new StringBuilder();
		if (upgrade_range == 0) {
			List<Map> list = service.getDeviceList(product_id, oldversion,devices);
			for (int i = 0; i < list.size(); i++) {
				if (sb.length() > 0) {
					sb.append(",");
				}
				sb.append(list.get(i).get("device_id"));
			}
			firmwareUpgradeMode.setDeviceId(sb.toString());
		} else {
			firmwareUpgradeMode.setDeviceId(device_list);
		}
		// upgradetime_type 0 現在更新  1 指定時間
		SimpleDateFormat today = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		if (upgradetime_type == 0) {
			firmwareUpgradeMode.setUpgradePlanTime(today.format(new Date()));
		} else {
			long currentTime = System.currentTimeMillis() + upgradetime * 60 * 1000;
			Date date = new Date(currentTime);
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String nowTime="";
			nowTime= df.format(date);
			firmwareUpgradeMode.setUpgradePlanTime(nowTime);
		}
		firmwareUpgradeMode.setRetryTimes(retrytime);
		firmwareUpgradeMode.setRetryInterval(retry);
		firmwareUpgradeMode.setIsDelete(0);
		firmwareUpgradeMode.setUpgradeState(0);
		firmwareUpgradeMode.setCutSize(cut_size);

		if(firmwareUpgradeMode.getDeviceId().equals("")){
			return R.fail("正在升级中，请勿重复升级");
		}else {
			FirmwareUpgradeMode flag = service.saveUpgradeMetnod(firmwareUpgradeMode);
			/**
			 * 向记录表中插入数据
			 */
			service.insertFirmwareRecord(flag, oldversion);
			return R.data("插入操作已完成");
		}


	}
}
